package com.huxiu;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.huxiu.net.GsonRequest;
import com.huxiu.net.NetworkConstants;
import com.huxiu.net.NetworkHelper;
import com.huxiu.net.model.BindInfoData;
import com.huxiu.net.responses.BindInfoResponse;
import com.huxiu.utils.Constants;
import com.huxiu.utils.Global;
import com.huxiu.utils.LogUtil;
import com.huxiu.utils.Utils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.tencent.mm.sdk.modelmsg.SendAuth.Req;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tian on 15/5/3:下午8:44.
 */
public class BindAccountsActivity extends AuthenticatorActivity {

    public static BindAccountsActivity instance;
    //    @ViewInject(R.id.back)
    //    ImageView back;

    @ViewInject(R.id.bind_weibo)
    TextView bindWeibo;

    @ViewInject(R.id.bind_weixin)
    TextView bindWeixin;

    @ViewInject(R.id.bind_qq)
    TextView bindQq;

    private int TYPE_QQ = -1;
    private int TYPE_SINNA = -1;
    private int TYPE_WEIXIN = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_accounts);
        ViewUtils.inject(this);
        loadData();
        //注册广播接收者，并添加多个过滤器
        IntentFilter intent = new IntentFilter();
        intent.addAction(Constants.INTENT_USER_BIND_OK);
        LocalBroadcastManager.getInstance(APP.getInstance()).registerReceiver(mReceiver, intent);
    }


    private List<BindInfoData.BindInfo> lists = new ArrayList<>();

    private void loadData() {
        String url = NetworkConstants.GET_USER_BINDINFO_URL;
        Map<String, String> params = new LinkedHashMap<>();
        params.put("uid", Global.user.uid);
        GsonRequest<BindInfoResponse> request = new GsonRequest<>(url, Request.Method.POST,
                BindInfoResponse.class, false, params,
                new Response.Listener<BindInfoResponse>() {
                    @Override
                    public void onResponse(BindInfoResponse response) {
                        if (response.isSuccess()) {
                            Collections.addAll(lists, response.data.bindinfo);
                            for (BindInfoData.BindInfo info : lists) {
                                if ("qq".equals(info.from)) {
                                    bindQq.setText("已绑定");
                                    bindQq.setTextColor(0xffbbbbbb);
                                    TYPE_QQ = 1;
                                } else if ("sina".equals(info.from)) {
                                    bindWeibo.setText("已绑定");
                                    bindWeibo.setTextColor(0xffbbbbbb);
                                    TYPE_SINNA = 1;
                                } else if ("weixin".equals(info.from)) {
                                    bindWeixin.setText("已绑定");
                                    bindWeixin.setTextColor(0xffbbbbbb);
                                    TYPE_WEIXIN = 1;
                                }
                            }
                            updateUI();
                        } else {
                            Utils.showToast(response.error.message);
                        }
                    }
                }, mErrorListener);
        NetworkHelper.getInstance().addToRequestQueue(request);

    }

    //广播接收者
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, final Intent intent) {
            loadData();
            updateUI();
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LocalBroadcastManager.getInstance(APP.getInstance()).unregisterReceiver(mReceiver);
    }

    @OnClick({R.id.back, R.id.qq_button, R.id.weixin_button, R.id.weibo_button})
    public void mOnClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.weibo_button:
                if (TYPE_SINNA == 1) {
                    //                    Settings.clearSinaLogInInfo();
                    Utils.showToast("已经绑定过");
                } else {
                    mSsoHandler.authorize(this);
                }
                break;
            case R.id.qq_button:
                if (TYPE_QQ == 1) {
                    //                    Settings.clearQQLogInInfo();
                    Utils.showToast("已经绑定过");
                } else {
                    mTencent.login(this, "all", this);
                }
                break;
            case R.id.weixin_button:
                if (TYPE_WEIXIN == 1) {
                    Utils.showToast("已经绑定过");
                } else {
                    if (mWechatApi.isWXAppInstalled()) {
                        Req req = new Req();
                        req.scope = "snsapi_userinfo";
                        mWechatApi.sendReq(req);
                    } else {
                        Utils.showToast(R.string.weixin_not_installed);
                    }
                }
                break;
        }
    }

    @Override
    protected void updateUI() {
        bindWeibo.setText(TYPE_SINNA == 1 ? R.string.bind : R.string.unbind);
        //        bindWeibo.setSelected(Settings.isWeiboLoggedIn());
        if (TYPE_SINNA == 1) {
            bindWeibo.setTextColor(0xffbbbbbb);
        }
        bindQq.setText(TYPE_QQ == 1 ? R.string.bind : R.string.unbind);
        //        bindQq.setSelected(Settings.isQQLoggedIn());
        if (TYPE_QQ == 1) {
            bindQq.setTextColor(0xffbbbbbb);
        }
        bindWeixin.setText(TYPE_WEIXIN == 1 ? R.string.bind : R.string.unbind);
        //        bindWeixin.setSelected(Settings.isWeixinLoggedIn());
        if (TYPE_WEIXIN == 1) {
            bindWeixin.setTextColor(0xffbbbbbb);
        }
    }

    //没有解除绑定的功能
    //    private void unbind(String type) {
    //        Map<String, String> params = new LinkedHashMap<>();
    //        params.put("act", "user_bind");
    //        params.put("bind_type", type);
    //        //        params.put("token", accessToken.getToken());
    //        //        params.put("openid", accessToken.getUid());
    //        if (type.equals("weibo")) {
    //            params.put("openid", Settings.getWeiboUid());
    //        } else if (type.equals("huxiu")) {
    //            params.put("openid", Settings.getHxUserId());
    //        } else if (type.equals("qq")) {
    //            params.put("openid",Settings.getQQUid());
    //        } else if (type.equals("weixin")) {
    //            params.put("openid",Settings.getWeixinUid());
    //        }
    //        params.put("operation_type", "unbind");
    //        doBind(params);
    //    }


    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("SplashScreen");
        MobclickAgent.onResume(this);
    }
    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("SplashScreen");
        MobclickAgent.onPause(this);
    }

}
